layui.use(['form', 'table', 'layer', 'jquery'], function () {
    var $ = layui.jquery,
        form = layui.form,
        table = layui.table,
        layer = layui.layer;

    //产品库存
    let productCount = 0;

    //初始化产品类别数据
    $.get({
        url: "/api/sort/sortExistList", success: (resp) => {
            resp.forEach(r => {
                $("#productType").append("<option value='" + r.sid + "'>" + r.sname + "</option>");
            });
            form.render('select');
        }, dataType: "json"
    });


    form.on('select(productTypeFilter)', (data) => {
        //类别id
        let sid = $('#productType option:selected').val();
        //根据类别id查询产品列表
        if (!!sid) {
            $('.a-class').remove();
            $('.b-class').remove();
            $('.c-class').remove();
            $.get({
                url: "/api/product/existp?sid=" + sid, success: (resp) => {
                    resp.forEach(r => {
                        $("#productName").append("<option class='a-class' value='" + r.pid + '#' + r.pname + "'>" + r.pname + "</option>");
                    });
                    form.render('select');
                }, dataType: "json"
            });
        }
    });

    form.on('select(productNameFilter)', (data) => {
        //产品名称
        let pname = $('#productName option:selected').val().split('#')[1];

        if (!!pname) {
            $('.b-class').remove();
            $('.c-class').remove();
            $.get({
                url: "/api/product/productArea?pname=" + pname, success: (resp) => {
                    resp.forEach(r => {
                        $("#productArea").append("<option class='b-class' value='" + r.pid + "'>" + r.pArea + "</option>");
                    });
                    form.render('select');
                }, dataType: "json"
            });
        }
    });

    form.on('select(productAreaFilter)', (data) => {

        //产品id
        let pid = $('#productArea option:selected').val();

        if (!!pid) {
            $('.c-class').remove();
            $.get({
                url: "/api/productyear/" + pid + "", success: (resp) => {
                    resp.forEach(r => {
                        $("#productYear").append("<option class='c-class' value='" + r.yid + "'>" + r.yname + "</option>");
                        //初始化进货价格数据
                        $("#inPrice").val(r.yprice);
                    });
                    form.render('select');
                    //库存数量
                    $.get({
                        sync: false,
                        url: "/api/stock/" + pid + "", success: (resp) => {
                            productCount = resp.ksum;
                        }, dataType: "json"
                    });
                }, dataType: "json"
            });
        }
    });


    $("#productYear").on("click", () => {
    });

    function checkOutPrice() {
        let outPrice = parseInt($("#outPrice").val());
        let inPrice = parseInt($("#inPrice").val());
        if (outPrice < inPrice) {
            layer.msg("出货价格需大于进货价格");
            return false;
        }
        return true;
    }

    function checkBuyNum() {
        let $buynum = $("#buynum");
        if ($buynum.val() > productCount) {
            layer.msg("库存不足");
            return false;
        }
        return true;
    }

    //出货价格
    $("#outPrice").on("blur ", () => {
        checkOutPrice()
    });


    //购买数量
    $("#buynum").on("blur ", () => {
        checkBuyNum();
    });
});